using System;
using System.ComponentModel.Design;

namespace Needle.Utils.Extensions
{
    /// <summary>
    /// System.ComponentModel.Design.IServiceContainer extension methods
    /// </summary>
    public static class IServiceContainerExtensions
    {
        /// <summary>
        /// Generic implementation of IServiceContainer.AddService(Type serviceType, object instance) 
        /// </summary>
        public static void AddService<TService>(this IServiceContainer serviceContainer, TService instance)
            where TService : class
        {
            if (serviceContainer == null)
                throw new NullReferenceException("IServiceContainer instance is null");
            serviceContainer.AddService(typeof(TService), instance, false);
        }

        /// <summary>
        /// Generic implementation of IServiceContainer.AddService(Type serviceType, ServiceCreatorCallback serviceCreator) 
        /// </summary>
        public static void AddService<TService>(this IServiceContainer serviceContainer, ServiceCreatorCallback serviceCreator)
            where TService : class
        {
            if (serviceContainer == null)
                throw new NullReferenceException("IServiceContainer instance is null");
            serviceContainer.AddService(typeof(TService), serviceCreator, false);
        }

        /// <summary>
        /// Generic implementation of IServiceContainer.AddService(Type serviceType, object instance, bool promote)
        /// </summary>
        public static void AddService<TService>(this IServiceContainer serviceContainer, TService instance, bool promote)
            where TService : class
        {
            if (serviceContainer == null)
                throw new NullReferenceException("IServiceContainer instance is null");
            serviceContainer.AddService(typeof(TService), instance, promote);
        }

        /// <summary>
        /// Generic implementation of IServiceContainer.AddService(Type serviceType, ServiceCreatorCallback serviceCreator, bool promote)
        /// </summary>
        public static void AddService<TService>(this IServiceContainer serviceContainer, ServiceCreatorCallback serviceCreator, bool promote)
            where TService : class
        {
            if (serviceContainer == null)
                throw new NullReferenceException("IServiceContainer instance is null");
            serviceContainer.AddService(typeof(TService), serviceCreator, promote);
        }

        /// <summary>
        /// Generic implementation of IServiceContainer.RemoveService(Type serviceType, bool promote)
        /// </summary>
        public static void RemoveService<TService>(this IServiceContainer serviceContainer, bool promote)
            where TService : class
        {
            if (serviceContainer == null)
                throw new NullReferenceException("IServiceContainer instance is null");
            serviceContainer.RemoveService(typeof(TService), promote);
        }

        /// <summary>
        /// Generic implementation of IServiceContainer.RemoveService(Type serviceType)
        /// </summary>
        public static void RemoveService<TService>(this IServiceContainer serviceContainer)
            where TService : class
        {
            if (serviceContainer == null)
                throw new NullReferenceException("IServiceContainer instance is null");
            serviceContainer.RemoveService(typeof(TService), false);
        }
    }
}
